//
//  HPDropDownView.m
//  HPZZTong
//
//  Created by handpay on 14-1-13.
//  Copyright (c) 2014年 Handpay. All rights reserved.
//

#import "HPDropDownView.h"
#import "ZZTongDefine.h"
#import "HPButtonCustomer.h"
#import "HPPopView.h"

@interface HPDropDownView ()

@property (retain, nonatomic) HPButtonCustomer *selectBackCard;
@property (nonatomic, retain) HPPopView *popView;

@end

@implementation HPDropDownView

@synthesize selectBackCard;
@synthesize popView;


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.selectedId = -1;
        HPButtonCustomer* bankCard =[[HPButtonCustomer alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        self.selectBackCard = bankCard;
        [bankCard release];
        
        //按钮
        UIImage *bgImage = [UIImage imageNamed:@"input"];
        UIImage *resizeBgImage = [bgImage resizableImageWithCapInsets:UIEdgeInsetsMake(bgImage.size.height/2 - 1, bgImage.size.width/2 - 1, bgImage.size.height/2, bgImage.size.width/2)];
        [self.selectBackCard addTarget:self action:@selector(accountBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.selectBackCard setBackgroundImage:resizeBgImage forState:UIControlStateNormal];
       self.selectBackCard.exclusiveTouch = YES;
        [self addSubview:self.selectBackCard];
        
#if (ISJSBank) && (ISJSBank == 1)
        //弹出窗口
        HPPopView* popView_ = [[HPPopView alloc] initWithFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y + self.frame.size.height, self.frame.size.width, 144)];
#else
        HPPopView* popView_ = [[HPPopView alloc] initWithFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y + self.frame.size.height, self.frame.size.width, 100)];
#endif
        self.popView = popView_;
        [popView_ release];
        self.popView.backgroundColor = [UIColor clearColor];
        self.popView.delegate = self;
        self.popView.hidden = YES;
        
    }
    return self;
}
- (void)setPlaceholder:(NSString*)holder{
    self.selectBackCard.textFiled.placeholder = holder;
}
/**
 * 选择银行卡号
 */
- (void)accountBtnClick:(id)sender {
    
    if (self.popView.hidden) {
        self.popView.hidden = NO;
        
        if (!self.popView.superview) {
            [self.superview addSubview:self.popView];
        }
        [self.popView.superview bringSubviewToFront:self.popView];
        [self doDisableViews:YES];
    }
    else {
        self.popView.hidden = YES;
        [self doDisableViews:NO];
    }
}


/**
 * view 上得视图是否可以点击,YES 不可以点击 NO 可以点击
 */
- (void)doDisableViews:(BOOL)yesOrNo {
    if (yesOrNo) {
        for (UIView *view in [self.superview subviews]) {
            
            if (([view isKindOfClass:[self class]]) || ([view isKindOfClass:[HPPopView class]])) {
                view.userInteractionEnabled = YES;
            }
            else {
                view.userInteractionEnabled = NO;
            }
            
        }
    }
    else {
        for (UIView *view in [self.superview subviews]) {
            view.userInteractionEnabled = YES;
        }
    }
    
}

#pragma mark -
#pragma makr HPPopViewDelegate

- (void)popView:(HPPopView *)popView withSelectId:(int)Id {
    self.selectedId = Id;
    
    NSDictionary* dic =[self.popView.popTableDatas objectAtIndex:Id];
    
    [self.selectBackCard.iconImageView setImage:[UIImage imageNamed:[dic objectForKey:KEY_IMG_NAME]]];
    [self.selectBackCard.iconTitle setText:[dic objectForKey:KEY_BANK_NAME]];
    self.selectBackCard.textFiled.hidden = YES;
    self.popView.hidden = YES;
    //    _backView.alpha = 0.0f;
    [self doDisableViews:NO];
    
}

- (void)dealloc
{
    self.selectBackCard = nil;
    self.popView = nil;
    [super dealloc];
}

@end
